package com.shihan.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URI;

/**
 * @Description
 * @Author shihan
 * @Date 2020/12/26 20:41
 * @Version 1.0
 */
@Configuration
public class RabbitMQConfig {

    // 配置 ConnectionFactory 连接工厂
    @Bean
    public ConnectionFactory connectionFactory(){
        return new CachingConnectionFactory(URI.create("amqp://root:123456@192.168.66.129:5672/%2f"));
    }

    // 配置 RabbitTemplate
    @Bean
    @Autowired
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        return new RabbitTemplate(connectionFactory);
    }

    // 配置 RabbitAdmin
    @Bean
    @Autowired
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
        return new RabbitAdmin(connectionFactory);
    }

    // 消费者，只需要关注 Queue 队列即可
    @Bean
    public Queue queue(){
        return QueueBuilder.nonDurable("queue.anno.q01").build();
    }

}
